<?php

namespace App\Http\Controllers\Api;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;
use Tymon\JWTAuth\Facades\JWTAuth;

class AuthController extends ApiController
{
    /**
     * Create a new AuthController instance.
     * 要求附带email和password（数据来源users表）
     *
     * @return void
     */
//    public function __construct()
//    {
//        // 这里额外注意了：官方文档样例中只除外了『login』
//        // 这样的结果是，token 只能在有效期以内进行刷新，过期无法刷新
//        // 如果把 refresh 也放进去，token 即使过期但仍在刷新期以内也可刷新
//        // 不过刷新一次作废
//        $this->middleware('jwt.auth', ['except' => ['login']]);
//        // 另外关于上面的中间件，官方文档写的是『auth:api』
//        // 但是我推荐用 『jwt.auth』，效果是一样的，但是有更加丰富的报错信息返回
//    }

    /**
     * Get a JWT via given credentials.
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function login(Request $request)
    {
        $code = $request['code'];
        $encryptedData = $request['encryptedData'];
        $iv = $request['iv'];

        $data = [];
        $res = User::wxLogin($code,$encryptedData,$iv);
        if (!$res){
            return self::returnAjax('', 201, "请授权手机号");
        }
//        $res = User::find(1);
        $token = JWTAuth::fromUser($res);
        $data['token'] = 'Bearer'.$token;
        $data['id'] = $res->id;
        $data['user_phone'] = $res->user_phone;
        $data['user_nickname'] =  $res->user_nickname;
        $data['user_headimgurl'] = $res->user_headimgurl;
        $data['sport_atatus'] = $res->sport_status;
        $data['level'] = $res->level;
        return self::returnAjax($data, 200, "登陆成功");
    }

    /**
     * Get the authenticated User.
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function me()
    {
        return response()->json(auth('api')->user());
    }

    /**
     * Log the user out (Invalidate the token).
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function logout()
    {
        auth('api')->logout();

        return response()->json(['message' => 'Successfully logged out']);
    }

    /**
     * Refresh a token.
     * 刷新token，如果开启黑名单，以前的token便会失效。
     * 值得注意的是用上面的getToken再获取一次Token并不算做刷新，两次获得的Token是并行的，即两个都可用。
     * @return \Illuminate\Http\JsonResponse
     */
    public function refresh()
    {
        return $this->respondWithToken(auth('api')->refresh());
    }

    /**
     * Get the token array structure.
     *
     * @param  string $token
     *
     * @return \Illuminate\Http\JsonResponse
     */
    protected function respondWithToken($token)
    {
        return response()->json([
            'access_token' => $token,
            'token_type' => 'bearer',
            'expires_in' => auth('api')->factory()->getTTL() * 60
        ]);
    }
}
